using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DBSourceToolsLib.DatabaseProjectUI
{
    public partial class NewProjectForm : Form
    {
        private DBConnectionLib.Database m_Database = null;
        private DBSourceToolsLib.DatabaseProject m_DatabaseProject = null;

        public NewProjectForm()
        {
            InitializeComponent();
            
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            if (this.m_folderBrowserDialog1.ShowDialog(this) == DialogResult.OK)
            {
                this.m_textBoxBaseDirectory.Text = this.m_folderBrowserDialog1.SelectedPath;
                CheckOKVisible();
            }
        }

        private void btnSelectDatabase_Click(object sender, EventArgs e)
        {
            NewDatabaseForm oNewDbForm = new NewDatabaseForm(true, false);
            if (oNewDbForm.ShowDialog(this) == DialogResult.OK)
            {
                SetDatabase(oNewDbForm.GetDatabaseObject());
                this.m_textBoxDatabaseNick.Text = this.m_DatabaseProject.SourceDatabases[0].NickName;
                CheckOKVisible();
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            
        }

        private void CheckOKVisible()
        {
            if (String.IsNullOrEmpty(this.m_textBoxProjectName.Text))
            {
                return;
            }
            if (String.IsNullOrEmpty(this.m_textBoxBaseDirectory.Text))
            {
                return;
            }
            this.btnOk.Enabled = true;

        }

        private void NewProjectForm_Load(object sender, EventArgs e)
        {
            this.btnOk.Enabled = false;
        }

        private void m_textBoxProjectName_TextChanged(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(m_textBoxProjectName.Text))
            {
                SetProjectProperties(m_textBoxProjectName.Text, m_textBoxBaseDirectory.Text);
                this.btnBrowse.Enabled = true;
                this.m_textBoxBaseDirectory.Enabled = true;
            }
        }

        private void SetProjectProperties(string strProjectName, string strBaseDirectory)
        {
            if (m_DatabaseProject == null)
            {
                this.m_DatabaseProject = new DatabaseProject(strBaseDirectory,
                    strProjectName);
            }
            else
            {
                this.m_DatabaseProject.BaseDirectory = strBaseDirectory;
                this.m_DatabaseProject.ProjectName = strProjectName;
            }
        }

        private void m_textBoxBaseDirectory_TextChanged(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(m_textBoxBaseDirectory.Text))
            {
                this.btnSelectDatabase.Enabled = true;
                SetProjectProperties(m_textBoxProjectName.Text, m_textBoxBaseDirectory.Text);                
            }
        }

        private void m_textBoxDatabaseNick_TextChanged(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(m_textBoxDatabaseNick.Text))
            {
                this.btnOk.Enabled = true;
            }
        }

        public DatabaseProject GetDatabaseProject()
        {
            return this.m_DatabaseProject;
        }

        public void SetDatabase(DBConnectionLib.Database database)
        {
            if (this.m_DatabaseProject.SourceDatabases.Count > 0)
                this.m_DatabaseProject.SourceDatabases.Clear();
            this.m_DatabaseProject.AddNewDatabase(database);
        }
    }
}